﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace Communergy.Common.Factories
{


    public static class CommunergyUploadFactory
    {
        private static ICommunergyUpload _currentCostUpload;

        /// <summary
        /// 
        /// </summary>
        /// <returns></returns>
        public static ICommunergyUpload GetInstance()
        {
            if (_currentCostUpload == null)
            {
                _currentCostUpload = CreateInstance();
            }

            return _currentCostUpload;
        }


        private static ICommunergyUpload CreateInstance()
        {
            ICommunergyUpload returnType = null;
            try
            {
                string useImplementation = ConfigurationSettings.AppSettings["GetCommunergyUploadImplementationType"];

                string[] typeinfo = useImplementation.Split(new char[] { ',' }, 2);

                Assembly wsImplAssembly = Assembly.Load(typeinfo[1]);
                string className = typeinfo[0];

                Type implType = wsImplAssembly.GetType(className, false, true);
                if (implType.IsClass && !implType.IsAbstract
                    && !implType.IsInterface && typeof(ICommunergyUpload).IsAssignableFrom(implType))
                {
                    returnType = (ICommunergyUpload)Activator.CreateInstance(implType);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Error creating ICommunergyUpload", ex);
            }

            return returnType;
        }
    }
}
